#!/usr/bin/env node

var fs    = require('fs')
var spawn = require('child_process').spawn

const LINK_PATH = 'node_modules/kurento-client'

fs.unlink(LINK_PATH, function(error)
{
  if(error && error.code != 'ENOENT') console.warn(error);

  fs.symlinkSync('..', LINK_PATH)

  var kurentoClient = process.env.KURENTOCLIENT_COV ? './lib-cov':'./lib'

  var argv =
  [
    '-c', 'kurentoClient:'+kurentoClient,
    '-c', 'node_modules/qunit-reporter-junit',
    '-c', 'wock:node_modules/wock',
    '-c', 'test/_common.js',
    '-c', 'test/_proxy.js',
    'test/','test/core','test/elements','test/filters', 'test/transactions',
    '--prefix','kurento.'
  ]

  spawn('qunit-cli', argv.concat(process.argv.slice(2)), {stdio: 'inherit'})
  .on('exit', function(code)
  {
    if(code)
      process.exit(code)

    argv =
    [
      '-c', 'kurentoClient:'+kurentoClient,
      '-c', 'node_modules/qunit-reporter-junit',
      '-c', 'wock:node_modules/wock',
      '-c', 'test/_common.js',
      '-c', 'test/_proxy.js',
      'test/','test/core','test/elements','test/filters', 'test/transactions',
      '--prefix','kurento.'
    ]
    spawn('qunit-cli', argv.concat(process.argv.slice(2)), {stdio: 'inherit'})
    .on('exit', function(code)
    {
      fs.unlink(LINK_PATH);
      if(code) process.exit(code)
    });
  });
})
